package yl.hs.bmipfull.controllers.laboratorydata;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.laboratorydata.ILabConfigDesignReportRecordsService;
import yl.hs.bmipfull.service.laboratorydata.ILabConfigDesignReportRmsService;
import yl.hs.bmipfull.service.laboratorydata.ILabConfigDesignReportService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴高丰
 * @apiNote 配比设计报告信息控制层
 * @since 2022年07月19日 16时11分27秒
 */
@Api(value = "配比设计报告信息Web接口", tags = "管理--配比设计报告信息操作")
@RequestMapping(value = "/api/lab/config/report")
@RestController
public class LabConfigDesignReportController extends ControllerBase {

    @Autowired
    ILabConfigDesignReportService service;
    @Autowired
    ILabConfigDesignReportRmsService rmsservice;
    @Autowired
    ILabConfigDesignReportRecordsService recodsservice;

    @ApiOperation(value = "查询配比设计报告信息", notes = "查询配比设计报告信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询配比设计报告信息", notes = "分页查询配比设计报告信息")
    @RequestMapping(value = "/query/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询配比设计报告信息", notes = "根据配比设计报告信息编号查询配比设计报告信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }
    @ApiOperation(value = "查询报告配比明细信息", notes = "根据配比设计报告信息编号查询报告配比明细信息")
    @RequestMapping(value = "/rms/{reportSN}", method = RequestMethod.GET)
    public WebResult findByRms(@ApiParam(value = "编号") @PathVariable String reportSN) {
        Map<String,Object> result = new HashMap<>();
        var rms = rmsservice.findBySn(reportSN);
        var report = recodsservice.findBySn(reportSN);
        result.put("subData",rms);
        result.put("formItem",report);
        return success("查询成功", result);
    }
    @ApiOperation(value = "查询报告试件实验信息", notes = "根据主表编号查询报告试件实验")
    @RequestMapping(value = "/report/{reportSN}", method = RequestMethod.GET)
    public WebResult findByReport(@ApiParam(value = "编号") @PathVariable String reportSN) {
        return success("查询成功", recodsservice.findBySn(reportSN));
    }
    @ApiOperation(value = "保存配比设计报告信息", notes = "查询/更新配比设计报告信息信息")
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    public WebResult saveLabConfigDesignReport(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveLabConfigDesignReport(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }
    @ApiOperation(value = "删除配比设计报告信息信息", notes = "根据配比设计报告信息编号删除系配比设计报告信息信息")
    @RequestMapping(path = "/delete/{sn}", method = RequestMethod.DELETE)
    public WebResult delete( @PathVariable String sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "查询原材检测信息", notes = "根据主表编号查询原材检测")
    @RequestMapping(value = "/querysn/{sn}", method = RequestMethod.GET)
    public WebResult querySn(@ApiParam(value = "编号") @PathVariable String sn) {

        return success("查询成功", recodsservice.findBySn(sn));
    }
}